package org.dizem.intellipainter.component.color;

import javax.swing.*;
import java.awt.*;

/**
 * User: dizem@126.com
 * Time: 12-2-18 下午3:17
 */
public class ColorButton extends JToggleButton {
	private Color color;
	private final String text;
	private static final int BORDER_WIDTH = 5;
	private static final Font font = new Font("微软雅黑", Font.PLAIN, 13);
	private static final Dimension size = new Dimension(45, 70);

	public ColorButton(Color color, String text, boolean selected) {
		this.color = color;
		this.text = text;
		setDoubleBuffered(true);
		setSelected(selected);
	}

	@Override
	public Dimension getPreferredSize() {
		return size;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHints(qualityHints);
		int labelWidth = getWidth() - BORDER_WIDTH * 2;
		g.setFont(font);
		g.setColor(Color.BLACK);
		g.drawString(text, BORDER_WIDTH, 65);
		g.setColor(Color.GRAY);
		g.drawRect(BORDER_WIDTH, BORDER_WIDTH, labelWidth, labelWidth);
		g.setColor(color);
		g.fillRect(BORDER_WIDTH + 2, BORDER_WIDTH + 2, labelWidth - 3, labelWidth - 3);
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
		repaint();
	}
}